//
//  PayTypeView.m
//  PigMarket
//
//  Created by mac on 2019/5/14.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "PayTypeView.h"

@interface PayTypeView ()
/**
 payTypeTitle
 */
@property (nonatomic , strong) UILabel *payTypeTitle;

/**
 line
 */
@property (nonatomic , strong) UIView *line1;
/**
 line
 */
@property (nonatomic , strong) UIView *line2;
/**
 wxicon
 */
@property (nonatomic , strong) UIImageView *wxIcon;
/**
 wxTitle
 */
@property (nonatomic , strong) UILabel *wxTitle;
/**
 wxbg
 */
@property (nonatomic , strong) UIView *wxbg;
/**
 wxSelectBtn
 */
@property (nonatomic , strong) UIButton *wxSelectBtn;
/**
 alipayIcon
 */
@property (nonatomic , strong) UIImageView *alipayIcon;
/**
 alipayTitle
 */
@property (nonatomic , strong) UILabel *alipayTitle;
/**
 alipaybg
 */
@property (nonatomic , strong) UIView *alipaybg;
/**
 alipaySelectBtn
 */
@property (nonatomic , strong) UIButton *alipaySelectBtn;
@end
@implementation PayTypeView

#pragma mark - init method
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
        [self initLayout];
        [self initAction];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self addSubview:self.payTypeTitle];
    [self addSubview:self.cancelBtn];
    [self addSubview:self.line1];
    
    [self addSubview:self.wxbg];
    [self.wxbg addSubview:self.wxIcon];
    [self.wxbg addSubview:self.wxTitle];
    [self.wxbg addSubview:self.wxSelectBtn];
    [self addSubview:self.line2];
    
    [self addSubview:self.alipaybg];
    [self.alipaybg addSubview:self.alipayIcon];
    [self.alipayIcon addSubview:self.alipayTitle];
    [self.alipaybg addSubview:self.alipaySelectBtn];
}

- (void)initLayout {
    // 布局子控件
    [self.payTypeTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(15);
        make.height.mas_equalTo(22);
    }];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.payTypeTitle.mas_centerY);
        make.height.mas_equalTo(22);
    }];
    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.payTypeTitle.mas_bottom).offset(15);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    [self.wxbg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.line1.mas_bottom).offset(0);
        make.height.mas_equalTo(54);
    }];
    [self.wxIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(self.wxbg.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(18, 18));
    }];
    [self.wxTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.wxIcon.mas_centerY);
        make.left.mas_equalTo(self.wxIcon.mas_right).offset(15);
        make.height.mas_equalTo(20);
    }];
    [self.wxSelectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.wxIcon.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(18, 18));
    }];
    [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.wxTitle.mas_left);
        make.top.mas_equalTo(self.wxbg.mas_bottom);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    
    [self.alipaybg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.line2.mas_bottom);
        make.height.mas_equalTo(54);
    }];
    [self.alipayIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(self.alipaybg.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(19, 19));
    }];
    [self.alipayTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.alipaybg.mas_centerY);
        make.left.mas_equalTo(self.alipayIcon.mas_right).offset(15);
        make.height.mas_equalTo(20);
    }];
    [self.alipaySelectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.alipayIcon.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(18, 18));
    }];
}
- (void)initAction {
    UITapGestureRecognizer *wxTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(wxTapped)];
    
    [self.wxbg addGestureRecognizer:wxTap];
    
    UITapGestureRecognizer *alipayTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alipayTapped)];
    [self.alipaybg addGestureRecognizer:alipayTap];
    
}
- (void)wxTapped {
    self.wxSelectBtn.selected = !self.wxSelectBtn.selected;
    self.alipaySelectBtn.selected = NO;
    if (self.payTypeBlock && self.wxSelectBtn.selected) {
        self.payTypeBlock(PayTypeWechat);
    }
}
- (void)alipayTapped {
    self.alipaySelectBtn.selected = !self.alipaySelectBtn.selected;
    self.wxSelectBtn.selected = NO;
    if (self.payTypeBlock && self.alipaySelectBtn.selected) {
        self.payTypeBlock(PayTypeAlipay);
    }
}

#pragma mark - getter and setter
- (UILabel *)payTypeTitle {
    if (!_payTypeTitle) {
        _payTypeTitle = [[UILabel alloc] init];
        _payTypeTitle.textColor = [UIColor textBlackColor];
        _payTypeTitle.font = [UIFont largerFont];
        _payTypeTitle.text = @"选择支付方式";
    }
    return _payTypeTitle;
}
- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor colorWithHexString:@"#24253D"] forState:UIControlStateNormal];
        _cancelBtn.titleLabel.font = [UIFont largerFont];
    }
    return _cancelBtn;
}
- (UIView *)line1 {
    if (!_line1) {
        _line1 = [[UIView alloc] init];
        _line1.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.08];
    }
    return _line1;
}
- (UIView *)line2 {
    if (!_line2) {
        _line2 = [[UIView alloc] init];
        _line2.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.08];
    }
    return _line2;
}
- (UIView *)wxbg {
    if (!_wxbg) {
        _wxbg = [[UIView alloc] init];
    }
    return _wxbg;
}
- (UIImageView *)wxIcon {
    if (!_wxIcon) {
        _wxIcon = [[UIImageView alloc] init];
        _wxIcon.image = [UIImage imageNamed:@"pay_weichat"];
    }
    return _wxIcon;
}
- (UILabel *)wxTitle {
    if (!_wxTitle) {
        _wxTitle = [[UILabel alloc] init];
        _wxTitle.textColor = [UIColor colorWithHexString:@"#333333"];
        _wxTitle.font = [UIFont systemFontOfSize:15];
        _wxTitle.text = @"微信支付";
    }
    return _wxTitle;
}
- (UIView *)alipaybg {
    if (!_alipaybg) {
        _alipaybg = [[UIView alloc] init];
    }
    return _alipaybg;
}
- (UIImageView *)alipayIcon {
    if (!_alipayIcon) {
        _alipayIcon = [[UIImageView alloc] init];
        _alipayIcon.image = [UIImage imageNamed:@"pay_alipay"];
    }
    return _alipayIcon;
}
- (UILabel *)alipayTitle {
    if (!_alipayTitle) {
        _alipayTitle = [[UILabel alloc] init];
        _alipayTitle.textColor = [UIColor colorWithHexString:@"#333333"];
        _alipayTitle.font = [UIFont systemFontOfSize:15];
        _alipayTitle.text = @"支付宝支付";
    }
    return _alipayTitle;
}
- (UIButton *)wxSelectBtn {
    if (!_wxSelectBtn) {
        _wxSelectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_wxSelectBtn setBackgroundImage:[UIImage imageNamed:@"pay_default"] forState:UIControlStateNormal];
        [_wxSelectBtn setBackgroundImage:[UIImage imageNamed:@"pay_default_selected"] forState:UIControlStateSelected];

    }
    return _wxSelectBtn;
}
- (UIButton *)alipaySelectBtn {
    if (!_alipaySelectBtn) {
        _alipaySelectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_alipaySelectBtn setBackgroundImage:[UIImage imageNamed:@"pay_default"] forState:UIControlStateNormal];
        [_alipaySelectBtn setBackgroundImage:[UIImage imageNamed:@"pay_default_selected"] forState:UIControlStateSelected];
    }
    return _alipaySelectBtn;
}
@end
